/**
 * v-permit 操作权限处理
 */

import { ButtonPermis } from '@/store/piniaStore/buttonPermis';
import { getNavIdByFullpath } from '@/utils/common';
import { lodashEmpty } from '@/utils/lodashUtil';


export default {
  async inserted(el, binding, vnode) {
    const store = ButtonPermis();
    const { value } = binding;

    var fullPath = location.pathname + location.search;
    var navId = getNavIdByFullpath(fullPath);
    // console.log('navId',navId);
    /*  if (!navId /!*没有navId 拥有所有的权限*!/) {
        return false;
      }*/

    var buttons = [];
    if (navId) {
      buttons = store.getButtons(navId);
      // console.log("-> 获取到的权限是 navId buttons",navId, buttons);
      // console.log('navId buttons',buttons);
    }

    if (lodashEmpty(buttons)) {
      try {
        $(el).addClass('permitbutton').css({ display: 'none' });

      } catch (e) {
        console.error(e);
      }
    }
    const hasPermissions = buttons.some(str => {
      return str === value;
    });
    //console.log('v-permit value==>hasPermissions', value, hasPermissions);

    // console.log("hasPermissions",hasPermissions);
    if (!hasPermissions) {
      try {
        $(el).addClass('permitbutton').css({ display: 'none' });

      } catch (e) {
        console.error(e);
      }
    }
  },
};
